package org.huqiwen.blog.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.huqiwen.blog.data.LinkDataBean;
import org.huqiwen.blog.logic.LinkLogicBean;

public class LinkActionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = (String) request.getParameter("method");
		if (method == null) {
			PrintWriter out = response.getWriter();
			out.println("无效的请求");
		} else if (method.equals("addLink")) {
			AddLink(request, response);
		} else if (method.equals("delLink")) {
			DelLink(request, response);
		} else if (method.equals("editorLink")) {
			EditorLink(request, response);
		} else if (method.equals("saveEditor")) {
			SaveLink(request, response);
		}
	}

	protected void AddLink(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String linkName = request.getParameter("name");
		String linkUrl = request.getParameter("url");
		String linkDes = request.getParameter("desceription");
		int result = new LinkLogicBean().addLink(linkName, linkUrl, linkDes);
		if (result == 1) {
			response.sendRedirect("/MyBlogDemo/admin/editorLink.jsp");
		} else {
			response.sendRedirect("/MyBlogDemo/error.jsp");
		}
	}

	protected void DelLink(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int result = new LinkLogicBean().delLink(id);
		if (result == 1) {
			response.sendRedirect("/MyBlogDemo/admin/editorLink.jsp");
		} else {
			response.sendRedirect("/MyBlogDeom/error.jsp");
		}
	}

	protected void EditorLink(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		LinkDataBean bean = new LinkLogicBean().findLinkById(id);
		if (bean != null) {
			// request.getSession().setAttribute("linkBean", bean);
			request.setAttribute("linkBean", bean);
			request.getRequestDispatcher("admin/adminLink.jsp").forward(
					request, response);
			// response.sendRedirect("/MyBlogDemo/admin/adminLink.jsp");
		} else {
			response.sendRedirect("/MyBlogDemo/error.jsp");
		}
	}

	protected void SaveLink(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String linkName = request.getParameter("name");
		String linkUrl = request.getParameter("url");
		String linkDes = request.getParameter("desceription");
		int result = new LinkLogicBean().updateLink(id, linkName, linkUrl,
				linkDes);
		if (result == 1) {
			response.sendRedirect("admin/editorLink.jsp");
		} else {
			response.sendRedirect("error.jsp");
		}
	}
}
